package com.computergames.server.service.matcher;

import com.computergames.server.domain.MatchingPlayer;
import com.computergames.server.domain.MatchingType;
import com.computergames.server.config.Config;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MatchingManager {
    private static final MatchingManager INSTANCE = new MatchingManager(Config.getThreadCount());

    private final Map<MatchingType, Matcher> matchers;
    private final int threadCount;

    private MatchingManager(int threadCount) {
        this.threadCount = threadCount;
        this.matchers = new ConcurrentHashMap<>();
        for (MatchingType type : MatchingType.values()) {
            matchers.put(type, new Matcher(type, threadCount));
        }
    }

    public static MatchingManager getInstance() {
        return INSTANCE;
    }

    public void addPlayerToQueue(MatchingPlayer player) {
        Matcher matcher = matchers.get(player.getMatchingType());
        if (matcher != null) {
            matcher.addPlayer(player);
        }
    }

    public void shutdown() {
        for (Matcher matcher : matchers.values()) {
            matcher.shutdown();
        }
    }
}
